package org.easymis.oa.modules.main.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.easymis.commons.web.page.SimplePage;
import org.easymis.entitys.mybatis.dto.FlowRun;
import org.easymis.entitys.mybatis.dto.FlowType;
import org.easymis.entitys.mybatis.dto.HrmHolidayType;
import org.easymis.entitys.mybatis.dto.Member;
import org.easymis.entitys.mybatis.dto.OaAttendHolid;
import org.easymis.entitys.mybatis.dto.OrganizeFunction;
import org.easymis.oa.service.datacenter.FlowRunMng;
import org.easymis.oa.service.datacenter.FlowTypeMng;
import org.easymis.oa.service.datacenter.OrganizeFunctionMng;
import org.easymis.oa.service.hrm.HrmHolidayTypeMng;
import org.easymis.oa.service.main.OaAttendHolidMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * @see 请假管理
 *
 */
@Controller
@RequestMapping("/attend/holid/")
public class OaAttendHolidAction {
	@Resource
	private OaAttendHolidMng service;
	@Autowired
	private HrmHolidayTypeMng holidayTypeMng;
	@Resource
	private FlowRunMng flowRunMng;
	@Resource
	private FlowTypeMng flowTypeMng;
	@Autowired
	OrganizeFunctionMng organizeFunctionMng;

	@RequestMapping(value = "index")
	public String forwardaddressareaIndexJsp(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		model.put("breadcrumbList", breadcrumbList);
		return "attend/holid/index";
	}

	@RequestMapping(value = "grid", method = RequestMethod.POST)
	public String forwardList(Integer page, Integer pageSize, String flowTypeId, HttpServletRequest request) throws Exception {
		
		OaAttendHolid bean = new OaAttendHolid();
		//bean.setOrgId(orgId);
		List holidList=service.getList(bean);
		FlowRun flowRun = new FlowRun();
		flowRun.setFlowTypeId(flowTypeId);
		PageHelper.startPage(SimplePage.cpn(page), SimplePage.cps(pageSize));
		List<FlowRun> flowRun_list=flowRunMng.getList(flowRun);
		PageInfo<FlowRun> p = new PageInfo<FlowRun>(flowRun_list);
		
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		request.setAttribute("holidList", holidList);
		request.setAttribute("flowRun_list", flowRun_list);
		request.setAttribute("pageTotalResult", p.getTotal());
		
		return "attend/holid/grid";
	}

	@RequestMapping(value = "forwordEdit")
	public String forwardEdit(HttpServletRequest request, HttpSession session) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");
		String sysName=request.getParameter("sysName");
		FlowType flowType=flowTypeMng.findBySysName(sysName);		
		String flow_name=flowType.getTypeName()+"-"+userInfo.getStaffName()+"-"+"2017-11-06";
		HrmHolidayType bean = new HrmHolidayType();
		//bean.setOrgId(orgId);
		List holidayTypeList=holidayTypeMng.getList(bean);
		
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		request.setAttribute("flowTypeId", flowType.getFlowTypeId());
		request.setAttribute("flowName", flow_name);
		request.setAttribute("holidayTypeList", holidayTypeList);
		
		return "attend/holid/edit";
	}

	@RequestMapping(value = "forwordUpdate")
	public String forwardUpdate(HttpServletRequest request, HttpSession session) throws Exception {
		return "attend/holid/update";
	}

	@RequestMapping(value = "forwordView")
	public String forwardView(HttpServletRequest request, HttpSession session) throws Exception {
		return "attend/holid/view";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
	public void save(HttpServletRequest request, HttpSession session, FlowRun flowRun,OaAttendHolid bean) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");
/*		String flowTypeId=request.getParameter("flowTypeId");
		String flowName=request.getParameter("flowName");
		flowRun.setFlowName(flowName);
		flowRun.setFlowTypeId(flowTypeId);*/
		flowRun.setCreatorId(userInfo.getStaffId());		
		flowRun.setCreateTime(new Date());		
		flowRun.setOrgId(userInfo.getOrgId());
		flowRunMng.save(flowRun);
		
		bean.setCreateTime(flowRun.getCreateTime());
		bean.setCreatorId(userInfo.getStaffId());
		bean.setAttendHolidId(flowRun.getFlowRunId());
		bean.setOrgId(userInfo.getOrgId());
		bean.setStaffId(userInfo.getStaffId());
		
		service.save(bean);

	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void updateaddressarea(HttpServletRequest request, HttpSession session) throws Exception {
		String id = request.getParameter("id");
		OaAttendHolid bean = service.findById(id);
		service.update(bean);
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void delete(HttpServletRequest request, String id) throws Exception {
		flowRunMng.delete(id);		
		service.delete(id);
	}

	@RequestMapping(value = "deleteByIds", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	@ResponseBody
	public String deleteByIds(HttpServletRequest request) throws Exception {
		String ids_str = request.getParameter("id");
		String[] ids = ids_str.split(",");
		flowRunMng.deleteByIds(ids);
		service.deleteByIds(ids);
		
		return "";

	}

}
